package BookManagement.controller;

import BookManagement.domain.Book;
import BookManagement.domain.Comment;
import BookManagement.domain.Page;
import BookManagement.domain.User;
import BookManagement.service.BookService;
import BookManagement.service.CommentService;
import BookManagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    @Qualifier("commentService")
    private CommentService commentService;

    @Autowired
    @Qualifier("bookService")
    private BookService bookService;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    //评论
    @RequestMapping("/addComments")
    public ModelAndView addComments(String bookISBN, String content, HttpSession hs){
        ModelAndView mav=new ModelAndView();
        User u =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        if (u == null) {//未登录，跳转至登录界面
            mav.setViewName("/account/indexLogin");
            return mav;
        }
        Book b=bookService.findByISBN(bookISBN);
        HashMap<String,Object>map=new HashMap<>();
        map.put("bookISBN",bookISBN);map.put("username",u.getUserName());
        Comment c=commentService.findOnlyComment(map);
        if(c!=null){
            return new ModelAndView("/user/commentFail");
        }
        String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
        Comment comment=new Comment(u.getUserName(),bookISBN,b.getBookName(),u.getName(),content,time);
        commentService.addComment(comment);
        mav.setViewName("redirect:/book/findBookByISBN?bookISBN="+bookISBN);
        return mav;
    }

    //删除
    @RequestMapping("/delWithBookISBN/{UserName}")
    public ModelAndView deleteCommentWithBookAdmin(@PathVariable("UserName") String userName, String bookISBN){
        ModelAndView mav=new ModelAndView();
        HashMap<String,Object> map = new HashMap();
        map.put("bookISBN",bookISBN);
        map.put("userName",userName);
        commentService.deleteComment(map);
        mav.setViewName("redirect:/book/allCommentsWithBookAdmin?bookISBN="+bookISBN);
        return mav;
    }

    @RequestMapping("/delComment/{bookISBN}")
    public ModelAndView deleteCommentWithBook(@PathVariable("bookISBN") String  bookISBN,HttpSession hs){
        ModelAndView mav=new ModelAndView();
        HashMap<String,Object> map = new HashMap();
        User user =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        map.put("bookISBN",bookISBN);
        map.put("userName",user.getUserName());
        commentService.deleteComment(map);
        mav.setViewName("redirect:/comment/listAllComments");
        return mav;
    }

    //书籍底下列出评论
    public void listButton(ModelAndView mav,String bookISBN,CommentService commentService){
        Page p=new Page("1",5,commentService.findCommentCountForBook(bookISBN));
        HashMap<String,Object> map = new HashMap();
        map.put("bookISBN",bookISBN);
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Comment> commentsByBook = commentService.findCommentsByBook(map);
        mav.addObject("commentList",commentsByBook);
        mav.addObject("page",p);
    }

    @RequestMapping("/listAllComments")
    public ModelAndView listAllCommentsUser(HttpSession hs,String pageNo){
        ModelAndView mav=new ModelAndView();
        User u =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        if(u==null){
            return new ModelAndView("redirect:/account/toLogin");
        }
        Page p=new Page(pageNo,10,commentService.findCommentCountForUser(u.getUserName()));
        HashMap<String,Object> map = new HashMap();
        map.put("username",u.getUserName());
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Comment> commentList = commentService.findCommentsByUser(map);
        mav.addObject("commentList",commentList);
        mav.addObject("page",p);
        mav.addObject("author",u.getName());
        mav.setViewName("/user/allCommentsOfOneUser");
        return mav;
    }

    @RequestMapping("/listAllCommentsAdmin")
    public ModelAndView listAllCommentsUserAdmin(HttpSession hs,String pageNo,String username){
        ModelAndView mav=new ModelAndView();
        User u =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        if(u==null){
            return new ModelAndView("redirect:/account/toLogin");
        }
        Page p=new Page(pageNo,10,commentService.findCommentCountForUser(username));
        HashMap<String,Object> map = new HashMap();
        map.put("username",username);
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Comment> commentList = commentService.findCommentsByUser(map);
        mav.addObject("commentList",commentList);
        mav.addObject("page",p);
        mav.addObject("usernameC",username);
        mav.setViewName("/admin/users/userComment");
        return mav;
    }

    //展示书籍所有评论，管理员界面
    @RequestMapping("/allCommentsWithBookAdmin")
    public ModelAndView listBookComments(String pageNo,String bookISBN){
        ModelAndView mav=new ModelAndView();
        Page p=new Page(pageNo,10,commentService.findCommentCountForBook(bookISBN));
        Book book=bookService.findByISBN(bookISBN);
        HashMap<String,Object> map=new HashMap<>();
        map.put("bookISBN",bookISBN);
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Comment> commentsList = commentService.findCommentsByBook(map);
        mav.addObject("commentList",commentsList);
        mav.addObject("page",p);
        mav.addObject("book",book);
        mav.setViewName("admin/books/bookComment");
        return mav;
    }

    //展示书籍所有评论，用户界面
    @RequestMapping("/listComments")
    public ModelAndView listComments(String pageNo,String bookISBN){
        ModelAndView mav=new ModelAndView();
        Page p=new Page(pageNo,10,commentService.findCommentCountForBook(bookISBN));
        Book book=bookService.findByISBN(bookISBN);
        HashMap<String,Object> map=new HashMap<>();
        map.put("bookISBN",bookISBN);
        map.put("start",p.getStart());
        map.put("size",p.getPageSize());
        List<Comment> commentsList = commentService.findCommentsByBook(map);
        mav.addObject("commentList",commentsList);
        mav.addObject("page",p);
        mav.addObject("book",book);
        mav.setViewName("user/allCommentsOfOneBook");
        return mav;
    }

    @RequestMapping("/delCommentUAdmin")
    public ModelAndView deleteCommentWithUserAdmin(String  bookISBN,String username,String pageNo,HttpSession hs){
        ModelAndView mav=new ModelAndView();
        HashMap<String,Object> map = new HashMap();
        User user =userService.findUserByUsername( (String) hs.getAttribute("usercurrent"));
        map.put("bookISBN",bookISBN);
        map.put("userName",username);
        commentService.deleteComment(map);
        mav.setViewName("redirect:/comment/listAllCommentsAdmin?username="+username+"&pageNo="+pageNo);
        return mav;
    }
}
